package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DatabaseDetailsMapper;
import com.ruoyi.system.domain.DatabaseDetails;
import com.ruoyi.system.service.IDatabaseDetailsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 数据库Service业务层处理
 *
 * @author jw
 * @date 2022-06-15
 */
@Service
public class DatabaseDetailsServiceImpl implements IDatabaseDetailsService {
    @Autowired
    private DatabaseDetailsMapper databaseDetailsMapper;

    /**
     * 查询数据库
     *
     * @param databaseId 数据库主键
     * @return 数据库
     */
    @Override
    public DatabaseDetails selectDatabaseDetailsByDatabaseId(Long databaseId) {
        return databaseDetailsMapper.selectDatabaseDetailsByDatabaseId(databaseId);
    }

    /**
     * 查询数据库列表
     *
     * @param databaseDetails 数据库
     * @return 数据库
     */
    @Override
    public List<DatabaseDetails> selectDatabaseDetailsList(DatabaseDetails databaseDetails) {
        return databaseDetailsMapper.selectDatabaseDetailsList(databaseDetails);
    }

    /**
     * 新增数据库
     *
     * @param databaseDetails 数据库
     * @return 结果
     */
    @Override
    public int insertDatabaseDetails(DatabaseDetails databaseDetails) {
        return databaseDetailsMapper.insertDatabaseDetails(databaseDetails);
    }

    /**
     * 修改数据库
     *
     * @param databaseDetails 数据库
     * @return 结果
     */
    @Override
    public int updateDatabaseDetails(DatabaseDetails databaseDetails) {
        return databaseDetailsMapper.updateDatabaseDetails(databaseDetails);
    }

    /**
     * 批量删除数据库
     *
     * @param databaseIds 需要删除的数据库主键
     * @return 结果
     */
    @Override
    public int deleteDatabaseDetailsByDatabaseIds(String databaseIds) {
        return databaseDetailsMapper.deleteDatabaseDetailsByDatabaseIds(Convert.toStrArray(databaseIds));
    }

    /**
     * 删除数据库信息
     *
     * @param databaseId 数据库主键
     * @return 结果
     */
    @Override
    public int deleteDatabaseDetailsByDatabaseId(Long databaseId) {
        return databaseDetailsMapper.deleteDatabaseDetailsByDatabaseId(databaseId);
    }

    /**
     * 校验ip是否唯一
     *
     * @param databaseDetails 数据库信息
     * @return 结果
     */
    @Override
    public String checkIpUnique(DatabaseDetails databaseDetails) {

        Long databaseDetailId = StringUtils.isNull(databaseDetails.getDatabaseId()) ? -1L : databaseDetails.getDatabaseId();
        DatabaseDetails info = databaseDetailsMapper.checkIpUnique(databaseDetails.getIp());
        if (StringUtils.isNotNull(info) && !Objects.equals(info.getIp(), String.valueOf(databaseDetailId))) {
            return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;

    }

}